package com.xb.cloud.product.merchant.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.xb.cloud.common.util.DateUtils;
import com.xb.cloud.product.merchant.service.client.MerchantSmsSendLogService;
import com.xb.cloud.product.merchant.service.client.bo.SmsSendLogBo;
import com.xb.cloud.product.service.business.model.MerchantSmsSendLog;
import com.xb.cloud.product.service.business.model.MerchantSmsSendLogExample;
import com.xb.cloud.product.service.business.model.mapper.MerchantSmsSendLogMapper;
import com.xb.cloud.service.client.ServiceReturnCodes;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;
@Service
public class MerchantSmsSendLogServiceImpl extends AbstractService implements MerchantSmsSendLogService{
	
	@Resource
	MerchantSmsSendLogMapper merchantSmsSendLogMapper;
	
	
	@Override
	public ServiceReturnInfo countByPhoneNumWithDay(long companyId, String phoneNum) {
		MerchantSmsSendLogExample example = new MerchantSmsSendLogExample();
		String formateOfDay = DateUtils.formateDate(new Date());
		example.createCriteria().andSendDayEqualTo(formateOfDay).andCompanyIdEqualTo(companyId)
				.andPhoneNumEqualTo(phoneNum);
		int count = merchantSmsSendLogMapper.countByExample(example);
		return ServiceReturnUtils.success(count);
	}


	@Override
	public ServiceReturnInfo createSmsSendLog(SmsSendLogBo smsSendLogBo) {
		if (smsSendLogBo == null) {
			return ServiceReturnUtils.error(ServiceReturnCodes.PARAM_IS_NULL);
		}
		MerchantSmsSendLog smsSendLog = new MerchantSmsSendLog();
		BeanUtils.copyProperties(smsSendLogBo, smsSendLog);
		smsSendLog.setId(ipKeyGenerator.generateKey());

		smsSendLog.setSendDate(timeService.getDate());
		merchantSmsSendLogMapper.insert(smsSendLog);
		return ServiceReturnUtils.success();
	}

}
